
package org.springblade.modules.videoanswer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.videoanswer.vo.VideoBaseInfoVO;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.videoanswer.entity.Video;
import org.springblade.modules.videoanswer.vo.VideoVO;
import org.springblade.modules.videoanswer.service.IVideoService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;

/**
 * 视频管理 控制器
 *
 * @author jiangyandong
 * @since 2021-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("/videoanswer/video")
@Api(value = "视频管理", tags = "视频管理接口")
public class VideoController extends BladeController {

	private IVideoService videoService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入video")
	public R<Video> detail(Video video) {
		Video detail = videoService.getOne(Condition.getQueryWrapper(video));
		return R.data(detail);
	}

	/**
	 * 分页 视频管理
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入video")
	public R<IPage<Video>> list(Video video, Query query) {
		IPage<Video> pages = videoService.page(Condition.getPage(query), Condition.getQueryWrapper(video));
		return R.data(pages);
	}

	/**
	 * 自定义分页 视频管理
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入video")
	public R<IPage<VideoVO>> page(VideoVO video, Query query) {
		IPage<VideoVO> pages = videoService.selectVideoPage(Condition.getPage(query), video);
		return R.data(pages);
	}

	/**
	 * 新增 视频管理
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入video")
	public R save(@Valid @RequestBody Video video) {
		return R.status(videoService.save(video));
	}

	/**
	 * 修改 视频管理
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入video")
	public R update(@Valid @RequestBody Video video) {
		return R.status(videoService.updateById(video));
	}

	/**
	 * 新增或修改 视频管理
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入video")
	public R<Video> submit(@Valid @RequestBody Video video) {
		BladeUser loginUser = SecureUtil.getUser();
		if(video.getId() == null){
			video.setCreateUser(loginUser.getUserId());
		}
		video.setUpdateTime(new Date());
		video.setUpdateUser(loginUser.getUserId());
		videoService.saveOrUpdate(video);
		return R.data(video);
	}


	/**
	 * 删除 视频管理
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(videoService.removeByIds(Func.toIntList(ids)));
	}

	/**
	 * 详情（带题库、群组）
	 */
	@GetMapping("/detailVideo")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "详情（带题库、群组）", notes = "传入germplasm")
	public R<VideoBaseInfoVO> detailVideo(Video video){
		VideoBaseInfoVO vo = videoService.detailVideo(video);
		return R.data(vo);
	}
}
